package com.campus.campusdelivery.controller;

import com.campus.campusdelivery.dto.ApiResponse;
import com.campus.campusdelivery.dto.MenuItemDto;
import com.campus.campusdelivery.dto.MenuItemRequest;
import com.campus.campusdelivery.service.MenuItemService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/menu-items")
@RequiredArgsConstructor
@CrossOrigin(origins = "*")
public class MenuItemController {
    
    private final MenuItemService menuItemService;
    
    @GetMapping("/{id}")
    public ApiResponse<MenuItemDto> getMenuItemById(@PathVariable Long id) {
        try {
            MenuItemDto menuItem = menuItemService.getMenuItemById(id);
            return ApiResponse.success(menuItem);
        } catch (Exception e) {
            return ApiResponse.error(e.getMessage());
        }
    }
    
    @GetMapping("/merchant/{merchantId}")
    public ApiResponse<List<MenuItemDto>> getMenuItemsByMerchant(@PathVariable Long merchantId) {
        try {
            List<MenuItemDto> menuItems = menuItemService.getMenuItemsByMerchant(merchantId);
            return ApiResponse.success(menuItems);
        } catch (Exception e) {
            return ApiResponse.error(e.getMessage());
        }
    }
    
    @GetMapping("/merchant/{merchantId}/available")
    public ApiResponse<List<MenuItemDto>> getAvailableMenuItemsByMerchant(@PathVariable Long merchantId) {
        try {
            List<MenuItemDto> menuItems = menuItemService.getAvailableMenuItemsByMerchant(merchantId);
            return ApiResponse.success(menuItems);
        } catch (Exception e) {
            return ApiResponse.error(e.getMessage());
        }
    }
    
    @GetMapping("/category/{categoryId}")
    public ApiResponse<Page<MenuItemDto>> getMenuItemsByCategory(
            @PathVariable Long categoryId, Pageable pageable) {
        try {
            Page<MenuItemDto> menuItems = menuItemService.getMenuItemsByCategory(categoryId, pageable);
            return ApiResponse.success(menuItems);
        } catch (Exception e) {
            return ApiResponse.error(e.getMessage());
        }
    }
    
    @GetMapping("/search")
    public ApiResponse<Page<MenuItemDto>> searchMenuItems(
            @RequestParam String keyword, Pageable pageable) {
        try {
            Page<MenuItemDto> menuItems = menuItemService.searchMenuItems(keyword, pageable);
            return ApiResponse.success(menuItems);
        } catch (Exception e) {
            return ApiResponse.error(e.getMessage());
        }
    }
    
    @PostMapping("/merchant/{merchantId}")
    public ApiResponse<MenuItemDto> createMenuItem(
            @PathVariable Long merchantId, @Valid @RequestBody MenuItemRequest request) {
        try {
            MenuItemDto menuItem = menuItemService.createMenuItem(merchantId, request);
            return ApiResponse.success("创建成功", menuItem);
        } catch (Exception e) {
            return ApiResponse.error(e.getMessage());
        }
    }
    
    @PutMapping("/{id}")
    public ApiResponse<MenuItemDto> updateMenuItem(
            @PathVariable Long id, @Valid @RequestBody MenuItemRequest request) {
        try {
            MenuItemDto menuItem = menuItemService.updateMenuItem(id, request);
            return ApiResponse.success("更新成功", menuItem);
        } catch (Exception e) {
            return ApiResponse.error(e.getMessage());
        }
    }
    
    @DeleteMapping("/{id}")
    public ApiResponse<Void> deleteMenuItem(@PathVariable Long id) {
        try {
            menuItemService.deleteMenuItem(id);
            return ApiResponse.success("删除成功", null);
        } catch (Exception e) {
            return ApiResponse.error(e.getMessage());
        }
    }
    
    @PutMapping("/{id}/toggle-availability")
    public ApiResponse<Void> toggleMenuItemAvailability(@PathVariable Long id) {
        try {
            menuItemService.toggleMenuItemAvailability(id);
            return ApiResponse.success("状态切换成功", null);
        } catch (Exception e) {
            return ApiResponse.error(e.getMessage());
        }
    }
    
    @PutMapping("/{id}/stock")
    public ApiResponse<Void> updateMenuItemStock(
            @PathVariable Long id, @RequestParam Integer stock) {
        try {
            menuItemService.updateMenuItemStock(id, stock);
            return ApiResponse.success("库存更新成功", null);
        } catch (Exception e) {
            return ApiResponse.error(e.getMessage());
        }
    }
    
    @GetMapping("/count")
    public ApiResponse<Long> getMenuItemsCount() {
        try {
            Long count = menuItemService.getMenuItemsCount();
            return ApiResponse.success(count);
        } catch (Exception e) {
            return ApiResponse.error(e.getMessage());
        }
    }
}
